﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;

namespace Stote.UControls
{
    public partial class RoundedButton : Button
    {
        public RoundedButton()
        {
            InitializeComponent();
            // 采用双缓冲技术绘制
            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.Selectable | ControlStyles.SupportsTransparentBackColor | ControlStyles.UserPaint, true);
        }

        private bool isRadius = false;
        [Description("是否圆角")]
        public bool IsRadius
        {
            get { return isRadius; }
            set { isRadius = value; Refresh(); }
        }

        private int cornerRadius = 24;
        [Description("圆角角度")]
        public int ConerRadius
        {
            get { return cornerRadius; }
            set { cornerRadius = Math.Max(value, 1); Refresh(); }
        }

        private bool isShowRect = false;
        [Description("是否显示边框")]
        public bool IsShowRect
        {
            get { return isShowRect; }
            set { isShowRect = value; Refresh(); }
        }

        private Color rectColor = Color.FromArgb(220, 220, 220);
        [Description("边框颜色")]
        public Color RectColor
        {
            get { return rectColor; }
            set { rectColor = value; Refresh(); }
        }

        private int rectWidth = 1;

        [Description("边框宽度")]
        public int RectWidth
        {
            get { return rectWidth; }
            set { rectWidth = value; Refresh(); }
        }

        private Color fillColor = Color.Transparent;
        [Description("填充颜色")]
        public Color FillColor
        {
            get { return fillColor; }
            set { fillColor = value; Refresh(); }
        }

        private string btnText = "";
        [Description("文本")]
        public string BtnText
        {
            get { return btnText; }
            set { btnText = value; Refresh(); }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            if (!Visible) return;

            GraphicsPath graphicsPath = new GraphicsPath();
            graphicsPath.StartFigure();
            Rectangle clientRectangle = ClientRectangle;
            if (isRadius)
            {
                graphicsPath.AddArc(0, 0, cornerRadius, cornerRadius, 180f, 90f);
                graphicsPath.AddArc(clientRectangle.Width - cornerRadius-1 , 0, cornerRadius, cornerRadius, 270f, 90f);
                graphicsPath.AddArc(clientRectangle.Width - cornerRadius-1 , clientRectangle.Height - cornerRadius -1, cornerRadius, cornerRadius, 0f, 90f);
                graphicsPath.AddArc(0, clientRectangle.Height - cornerRadius - 1 , cornerRadius, cornerRadius, 90f, 90f);
                graphicsPath.CloseFigure();
            }
            else
            {
                if (!isShowRect)//绘制边框 无边框时 填充
                {
                    Rectangle rect = new Rectangle(0, 0, Width-1 , Height - 1 );
                    graphicsPath.AddRectangle(rect);
                }
                else
                {
                    graphicsPath.AddRectangle(clientRectangle);
                }
            }

            Graphics g = e.Graphics;
            g.SmoothingMode = SmoothingMode.AntiAlias;  //使绘图质量最高，即消除锯齿
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.FillPath(new SolidBrush(fillColor), graphicsPath);
            if (isShowRect)
            {
                g.DrawPath(new Pen(rectColor, rectWidth), graphicsPath);
            }

            // 获取字体的高度
            SizeF size = g.MeasureString(btnText, Font);
            float fontWidth = size.Width;
            float fontHeight = size.Height;
            PointF pointF = new PointF((Width - fontWidth) / 2F, (Height - fontHeight) / 2F);
            g.DrawString(btnText, Font, new SolidBrush(ForeColor), pointF);
        }
    }
}
